package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuConfig;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_PRE_ZHUI_BU_OR_TIAO_ZHAN,desc = "是否可以追捕/挑战")
public class DouLuoDaLuPreZhuiBuOrTiaoZhanCmd extends AbstractCommand {

    int type;

    long enemyId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DouLuoDaLuProto.DouLuoDaLuPreZhuiBuOrTiaoZhanReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuPreZhuiBuOrTiaoZhanReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        enemyId = reqMsg.getEnemyId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(type != 2 && type != 3){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;

        if ((ret = DouLuoDaLuMgr.activityInDuiJuePeriod()) != 0) return;

        DouLuoDaLuConfig config = DouLuoDaLuMgr.getConfig();

        String consume;
        if(type == 2){
            consume = config.ZHUIBU_ITEM_ID + "=" + config.ZHUIBU_ITEM_NUM;
        }else {
            consume = config.TIAOZHAN_ITEM_ID + "=" + config.TIAOZHAN_ITEM_NUM;
        }
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(consume))){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        //跨服判断
        player.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_PRE_ZHUI_BU_OR_TIAO_ZHAN, DouLuoDaLuProto.DouLuoDaLuPreZhuiBuOrTiaoZhanCrossReqMsg.newBuilder().setType(type)
                .setEnemyId(enemyId).setActivityId(DouLuoDaLuMgr.getActivityInfo().getActivityId()), DouLuoDaLuMgr.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_DOU_LUO_DA_LU_PRE_ZHUI_BU_OR_TIAO_ZHAN, DouLuoDaLuProto.DouLuoDaLuPreZhuiBuOrTiaoZhanRespMsg.newBuilder().setRet(ret));
        }
    }
}
